//
//  RadiusTableViewCell.m
//  OCPopular
//
//  Created by Jarvis on 2024/6/3.
//

#import "RadiusTableViewCell.h"
#import "Masonry.h"

@interface RadiusTableViewCell ()
@property (nonatomic, strong) UIView *radiusView;
@end
@implementation RadiusTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.radiusView];
    }
    return self;
}

- (void)didMoveToSuperview {
    [self.radiusView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView).inset(10);
        make.top.bottom.equalTo(self.contentView);
    }];
}

- (void)tableView:(UITableView *)tableView willDisplayAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger sections = [tableView numberOfRowsInSection:indexPath.section];
    [self.contentView layoutIfNeeded];
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.radiusView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(0, 0)];
    if (indexPath.row == 0 && indexPath.row == sections-1) {
        bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.radiusView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(_radius, _radius)];
    } else if (indexPath.row == 0) {
        bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.radiusView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(_radius, _radius)];
    } else if (indexPath.row == sections-1) {
        bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.radiusView.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(_radius, _radius)];
    }
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = bezierPath.CGPath;
    self.radiusView.layer.mask = shapeLayer;
}
- (UIView *)radiusView {
    if (!_radiusView) {
        _radiusView = [UIView new];
        _radiusView.backgroundColor = UIColor.whiteColor;
    }
    return _radiusView;
}
@end
